package com.wxzz.elearing.course.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wxzz.elearing.course.entity.Curriculum;
import com.wxzz.elearing.course.vo.DocentCurriculumStatisticsVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 课程 Mapper 接口
 * </p>
 *
 * @author
 * @since 2021-03-10
 */
public interface CurriculumMapper extends BaseMapper<Curriculum> {

    @Select("SELECT * FROM `curriculum` WHERE docent_id = #{docentId} AND deleted = 0")
    List<Curriculum> listCurriculumByDocentId(@Param("docentId") Long docentId);

    @Select("SELECT\n" +
            "\tCOUNT( 1 ) \n" +
            "FROM\n" +
            "\t( SELECT COUNT( 1 ) FROM `wx_user_curriculum_statistics_data` WHERE deleted = 0 AND save_time BETWEEN #{startTime} AND #{endTime} GROUP BY wx_user_id ) wx_user_curriculum_statistics_data")
    Integer selectCountStudyStatisticsVo(@Param("startTime") String startTime, @Param("endTime") String endTime);

    /**
     * 查询该讲师的参与学习人
     */
    @Select("SELECT COUNT(1) FROM curriculum c JOIN wx_user_curriculum wuc ON c.id = wuc.curriculum_id WHERE c.docent_id = #{docentId} AND c.deleted = 0 AND wuc.deleted = 0")
    Integer selectCountByDocentId(Long docentId);


    @Select("        SELECT\n" +
            "        COUNT(1)\n" +
            "        FROM\n" +
            "        curriculum c\n" +
            "        JOIN wx_user_curriculum wuc ON c.id = wuc.curriculum_id\n" +
            "        WHERE\n" +
            "        c.docent_id = #{docentId}\n" +
            "        AND c.deleted = 0\n" +
            "        AND wuc.STATUS = 1\n" +
            "        AND wuc.deleted = 0")
    Integer selectCountStudyNum(Long docentId);

    /**
     * 根据课程id查询章节的数量
     *
     * @param curriculumId
     * @return
     */
    @Select("SELECT COUNT(1) FROM curriculum c JOIN curriculum_section cs ON c.id = cs.curriculum_id WHERE c.id = #{curriculumId} AND c.deleted = 0 AND cs.deleted = 0")
    Integer selectCountByCurriculumId(@Param("curriculumId") Long curriculumId);

    /**
     * 查询用户完成对应课程的章节数量
     *
     * @param curriculumId
     * @param wxUserId
     * @return
     */
    @Select("SELECT\n" +
            "\tCOUNT( 1 ) \n" +
            "FROM\n" +
            "\tcurriculum c\n" +
            "\tJOIN curriculum_section cs ON c.id = cs.curriculum_id\n" +
            "\tJOIN wx_user_curriculum_section wucs ON cs.id = wucs.curriculum_section_id \n" +
            "WHERE\n" +
            "\twucs.wx_user_id = #{wxUserId} \n" +
            "\tAND wucs.is_pass = 1 \n" +
            "\tAND c.id = #{curriculumId} AND c.deleted = 0 AND cs.deleted = 0 AND wucs.deleted = 0 ")
    Integer selectIsPassByWxUserId(@Param("curriculumId") Long curriculumId, @Param("wxUserId") Long wxUserId);

    @Select("SELECT\n" +
            "\tc.* \n" +
            "FROM\n" +
            "\t`study_plan_curriculum` spc\n" +
            "\tJOIN curriculum c ON spc.curriculum_id = c.id \n" +
            "WHERE\n" +
            "\tspc.study_plan_unit_id = #{studyPlanUnitId} AND spc.deleted = 0 AND c.deleted = 0")
    List<Curriculum> listCurriculumByStudyPlanUnitId(@Param("studyPlanUnitId") Long studyPlanUnitId);

    List<DocentCurriculumStatisticsVo> selectDocentCurriculumStatisticsVo(@Param("enterpriseId") Long enterpriseId, @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("SELECT\n" +
            "\tSUM( study_time ) \n" +
            "FROM\n" +
            "\twx_user_curriculum_data \n" +
            "WHERE\n" +
            "\tcreate_time BETWEEN #{startTime} \n" +
            "\tAND #{endTime} \n" +
            "\tAND enterprise_id = #{enterpriseId} \n" +
            "\tAND deleted = 0")
    Long countCountStudyEnterpriseStatisticsVo(@Param("enterpriseId") Long enterpriseId, @Param("startTime") String startTime, @Param("endTime") String endTime);
}
